package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.common.exception.GuiguException;
import com.atguigu.spzx.common.util.AuthContextUtil;
import com.atguigu.spzx.manager.helper.MenuHelper;
import com.atguigu.spzx.manager.mapper.SysMenuMapper;
import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.model.entity.system.SysMenu;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.system.SysMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    SysMenuMapper sysMenuMapper;

    @Override
    public List<SysMenu> findNodes() {
        List<SysMenu> sysMenuList = sysMenuMapper.findAll();  //所有菜单，没组装父子关系前数据
        if(CollectionUtils.isEmpty(sysMenuList)){
            return null;
        }

        //所有菜单，但是，组装好父子关系，存储所有父节点。每个父的孩子都存储在children中。
        List<SysMenu> parentList = MenuHelper.buildTree(sysMenuList);

        return parentList;
    }

    @Override
    public void save(SysMenu sysMenu) {
        sysMenuMapper.insert(sysMenu);
    }

    @Override
    public SysMenu getById(Long id) {
        return sysMenuMapper.getById(id);
    }

    @Override
    public void updateById(SysMenu sysMenu) {
        sysMenuMapper.updateById(sysMenu);
    }

    @Override
    public void removeById(Long id) {
        //菜单有子节点不能删除的。

        //1.根据id查询子菜单，数量大于0不能删除
        int count = sysMenuMapper.countChildren(id);
        if(count>0){
            throw new GuiguException(ResultCodeEnum.NODE_ERROR);
        }
        //2.根据id查询子菜单，数量等于0可以删除
        sysMenuMapper.deleteById(id);
    }


    @Override
    public List<SysMenuVo> findUserMenuList() {
        //1.获取用户id (从线程上获取)
        SysUser sysUser = AuthContextUtil.get();

        //2.根据用户id查询拥有的菜单id集合
        List<SysMenu> selfMenuList =  sysMenuMapper.findSysMenuListByUserId(sysUser.getId());

        //3.组装菜单父子关系 - 递归
        List<SysMenu> parentMenuList = MenuHelper.buildTree(selfMenuList);

        return buildTree(parentMenuList);
    }

    //辅助方法：进行类型转换
    private List<SysMenuVo> buildTree(List<SysMenu> menuList) {
        List<SysMenuVo> sysMenuVoList = new ArrayList<>();

        for (SysMenu sysMenu : menuList) {
            SysMenuVo sysMenuVo = new SysMenuVo();
            sysMenuVo.setTitle(sysMenu.getTitle());
            sysMenuVo.setName(sysMenu.getComponent());
            List<SysMenu> children = sysMenu.getChildren();
            if(!CollectionUtils.isEmpty(children)){
                sysMenuVo.setChildren(buildTree(children));
            }
            sysMenuVoList.add(sysMenuVo);
        }

        return sysMenuVoList;
    }


}
